<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Promotion;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Promotion\Ctl\StatSqlCtl;
use Srv\Apps\Proto\DTO\Promotion\StatReportListDTO;
use Srv\Apps\Proto\DTO\Promotion\StatRetainListDTO;
use Srv\Apps\Proto\DTO\Promotion\StatSqlExecDTO;
use Srv\Apps\Proto\DTO\Promotion\StatSqlListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Promotion\StatDataJsonVO;
use Srv\Apps\Proto\VO\Promotion\StatReportListVO;
use Srv\Apps\Proto\VO\Promotion\StatRetainListVO;
use Srv\Apps\Proto\VO\Promotion\StatSqlListVO;
use Srv\Libs\Common\CommArray;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class StatSqlAct extends ActionAbstract
{
    /**
     * @url /stat/sql/list
     * @auth getStatSqlList
     * @req Srv.Apps.Proto.DTO.Promotion.StatSqlListDTO
     * @rsp Srv.Apps.Proto.VO.Promotion.StatSqlListVO
     * 统计sql列表
     */
    public function getStatSqlList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof StatSqlListDTO) || !($VOClass instanceof StatSqlListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = StatSqlCtl::tag()->getStatSqlList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /stat/data/list
     * @auth getStatDataList
     * @flag REQ_LOG=0
     * @req Srv.Apps.Proto.DTO.Promotion.StatSqlExecDTO
     * @rsp Srv.Apps.Proto.VO.Promotion.StatDataJsonVO
     * 统计数据结果
     */
    public function getStatDataList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof StatSqlExecDTO) || !($VOClass instanceof StatDataJsonVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $sqlrId                 = intval($DTOClass->getSqlrId());
        $paramsJson             = $DTOClass->getParamsJson();
        $StatSqlCtl             = StatSqlCtl::tag();
        $LogCtl                 = LogCtl::tag();
        $statSqlSrc             = $StatSqlCtl->getStatSqlSrc($sqlrId);
        $logData                = ['src' => $statSqlSrc, 'params' => $paramsJson];
        if(!isset($statSqlSrc['sqlr_id'])){
            $errMsg             = '统计SQL记录不存在';
            $LogCtl->logAdd(LogCtl::STAT_SQL_EXEC, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }else if(intval($statSqlSrc['sqlr_status']) === 0){
            $errMsg             = '统计SQL记录已停用';
            $LogCtl->logAdd(LogCtl::STAT_SQL_EXEC, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $optionsData            = $StatSqlCtl->getDataOptions($statSqlSrc, $paramsJson);
        if(!CommArray::isForeach($optionsData)){
            return ResponseData::rspSuccAuto('', null);
        }
        $VOClass                = $StatSqlCtl->getStatDataJsonStr($sqlrId, $optionsData);
        $errMsg                 = '统计SQL记录执行成功';
        $LogCtl->logAdd(LogCtl::STAT_SQL_EXEC, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', $VOClass);
    }


    /**
     * @url /stat/report/list
     * @auth statReportList
     * @flag REQ_LOG=0
     * @req Srv.Apps.Proto.DTO.Promotion.StatReportListDTO
     * @rsp Srv.Apps.Proto.VO.Promotion.StatReportListVO
     * 运营报表
     */
    public function getStatReportList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof StatReportListDTO) || !($VOClass instanceof StatReportListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $StatSqlCtl             = StatSqlCtl::tag();
        $VOClass                = $StatSqlCtl->getStatReportList($whereData);
        return ResponseData::rspSuccAuto('', $VOClass);
    }

    /**
     * @url /stat/retain/list
     * @auth statRetainList
     * @flag REQ_LOG=0
     * @req Srv.Apps.Proto.DTO.Promotion.StatRetainListDTO
     * @rsp Srv.Apps.Proto.VO.Promotion.StatRetainListVO
     * 统计留存
     */
    public function getStatRetainList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof StatRetainListDTO) || !($VOClass instanceof StatRetainListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $StatSqlCtl             = StatSqlCtl::tag();
        $VOClass                = $StatSqlCtl->getStatRetainList($whereData);
        return ResponseData::rspSuccAuto('', $VOClass);
    }
}